package com.khyr.service.impl;


import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.dao.CheckItemDao1;
import com.khyr.entity.CheckItem;

import com.khyr.service.CheckItemService1;
import com.khyr.vo.CheckItemVo;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 宋一恒
 * @version 1.0
 */
@Service
public class CheckItemServiceImpl1 implements CheckItemService1 {
    @Autowired
    private CheckItemDao1 checkItemDao;

    @Override
    public R<Page<CheckItem>> findCheckItemByCond(CheckItemVo checkItemVo) {
        // 分页查询
        Page<CheckItem> page = new Page<>(checkItemVo.getPageNum(), checkItemVo.getPageSize());
        //模糊
        QueryWrapper<CheckItem> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(checkItemVo.getCheckItemName())){
            queryWrapper.like("check_item_name", checkItemVo.getCheckItemName());
        }
        if (StringUtils.hasText(checkItemVo.getKeywords())){
            queryWrapper.like("keywords", checkItemVo.getKeywords());
        }
        if (StringUtils.hasText(checkItemVo.getTypeId())){
            queryWrapper.like("type_id", checkItemVo.getTypeId());
        }
        if (StringUtils.hasText(checkItemVo.getStatus())){
            queryWrapper.like("status", checkItemVo.getStatus());
        }
        if (StringUtils.hasText(checkItemVo.getBeginTime())&& StringUtils.hasText(checkItemVo.getEndTime())){
            queryWrapper.between("create_time", checkItemVo.getBeginTime(), checkItemVo.getEndTime());
        }
        Page<CheckItem> PageInfo = checkItemDao.selectPage(page, queryWrapper);
        if (PageInfo != null){
            return new R<>(200,"查询成功", PageInfo);
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<String> addCheckItem(CheckItem checkItem) {
        if (checkItemDao.insert(checkItem) > 0){
            return new R<>(200,"添加成功", null);
        }
        return new R<>(500,"添加失败", null);
    }

    @Override
    public R<String> updateCheckItem(CheckItem checkItem) {
        if (checkItemDao.updateById(checkItem) > 0){
            return new R<>(200,"修改成功", null);
        }
        return new R<>(500,"修改失败", null);
    }

    @Override
    public R<CheckItem> getCheckItemByCheckItemId(Long id) {
        if (checkItemDao.selectById(id) != null){
            return new R<>(200,"查询成功", checkItemDao.selectById(id));
        }
        return new R<>(500,"查询失败", null);
    }

    @Override
    public R<String> deleteCheckItemByIds(List<Long> ids) {
        if (ids != null){
            int i = checkItemDao.deleteIds(ids);
            if (i > 0){
                return new R<>(200,"删除成功", null);
            }
        }
        return new R<>(500,"删除失败", null);
    }
}
